#!/bin/bash
db_name="db_cmdb"
mysql_user="xxxxxx"
mysql_passwd="xxxxxx"
host_list="192.168.x.xxx"
mysql_port=3306
log_dir="log"
ha_list_file="${log_dir}/ha.log"
[ ! -d "${log_dir}" ] && mkdir ${log_dir}
monitor_file="monitor_mgr.sh"
mark_file=".mark"
[ -f "${mark_file}" ] && rm -f ${mark_file}
stop_file="stop_file"
[ -d "md5_res" ] && rm -f md5_res/*
mysql_comm="mysql -u${mysql_user} -p${mysql_passwd} -h${host_list} -P${mysql_port} ${db_name}"
while :
do
    [ -f "${mark_file}" ] && sleep 1 && continue
    [ -f "${stop_file}" ] && exit
    ${mysql_comm} -NBe "select distinct tdhg.ha_group_name, tdhgm.role_name,tsi.ip_app, tdi.port,tpdi.product_name, tpji.project_name from t_server_info tsi join t_db_instance tdi on tsi.server_id = tdi.server_id join t_db_ha_group_member tdhgm on tdhgm.member_id = tdi.instance_id join t_db_ha_group tdhg on tdhg.ha_group_id = tdhgm.ha_group_id join t_db_database tdd on tdd.instance_id = tdi.instance_id join t_project_info tpji on tdd.project_id = tpji.project_id join t_product_info tpdi on tpdi.product_id = tpji.product_id where 1=1 and ha_type='mgr' and tsi.ip_app like '192%' order by tdhg.ha_group_name;" 2>/dev/null |tr "\t" ":" > ${ha_list_file}
    for ha_name in $(grep ":写节点:" ${ha_list_file}|awk -F: '{print $1}'|sort|uniq)
    do
    #{
        while :
        do
            p_count=$(ps -ef|grep bash|grep -c ${monitor_file})
            [ ${p_count} -gt 50 ] && sleep 2 || break
        done
        touch ${mark_file}
        bash ${monitor_file} ${ha_name} &
    #}&
    done
    #wait
    sleep 5
done